package com.taptech.doufu.receiver;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.bean.TipInfo;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.listener.ResultTipListener;
import com.taptech.doufu.services.PersonalNoteService;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.CompetenceDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by lsy on 2015/8/13.
 */
public class CompetencePupopWindowReceiver extends BroadcastReceiver implements HttpResponseListener {

    Context mContext;
    String mObjectType;

    // 储存是否已弹窗表名
    static public final String COMPETENCE_SHAREDPREFERENCES = "competence_sharedpreference";
    // 储存是否已弹窗key
    static public final String PUPOPWINDOW_SAVE_KEY = "pupopwindow_save_key";

    // 广播action的key与值
    static public final String PUBLISH_NOVEL_ACTION = "publish_novel_action";
    static public final String PUBLISH_DRAW_ACTION = "publish_draw_action";
    static public final String PUBLISH_COMMAND_TYPE_KEY = "publish_command_type_key ";


    static public final int PUBLISH_COMMAND_TYPE_NOVEL = 1018;
    static public final int PUBLISH_COMMAND_TYPE_DRAW = 1042;
    static public final int PUBLISH_COMMAND_TYPE_COS = 1043;

    SharedPreferences competencePupopWindowSharedPreferences;
    SharedPreferences.Editor sharedPreferencesEditor;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.mContext = context;
        competencePupopWindowSharedPreferences = context.getSharedPreferences(COMPETENCE_SHAREDPREFERENCES, Activity.MODE_PRIVATE);
        sharedPreferencesEditor = competencePupopWindowSharedPreferences.edit();

        int command = intent.getIntExtra(PUBLISH_COMMAND_TYPE_KEY, 0);

        switch (command) {
            case PUBLISH_COMMAND_TYPE_NOVEL:
                mObjectType = String.valueOf(Constant.OBJECT_TYPE_NOVEL);
                break;
            case PUBLISH_COMMAND_TYPE_DRAW:
                mObjectType = String.valueOf(Constant.OBJECT_TYPE_DRAW);
                break;
            case PUBLISH_COMMAND_TYPE_COS:
                mObjectType = String.valueOf(Constant.OBJECT_TYPE_COS);
                break;
        }
        handleComtenceReceiver(mObjectType);
    }

    private void handleComtenceReceiver(String object_type) {
        boolean isNeedPopup = competencePupopWindowSharedPreferences.getBoolean(PUPOPWINDOW_SAVE_KEY + object_type, true);
        if (isNeedPopup) {
            PersonalNoteService.getInstance().isNeedPopWindowRequest(this, object_type);
        }
    }

    private void handleComtenceRequest() {

    }

    private ResultTipListener tipListener = new ResultTipListener() {

        @Override
        public void resultBack(int flag, int type) {
            // TODO Auto-generated method stub
            if (flag == 1) {
                Intent intent = new Intent(mContext, BrowseActivity.class);
                intent.putExtra(Constant.URL, "http://api.doufu.la/index.php/web/requests/index?object_type=" + mObjectType);
                intent.putExtra(Constant.TITLE, "权限申请");
                mContext.startActivity(intent);
            }
            // 作者选择不再弹窗
            else if (flag == 2){
                TTLog.d("tag" , "弹窗的object_type = +++=====" + mObjectType );
                switch (Integer.valueOf(mObjectType)) {
                    case Constant.OBJECT_TYPE_NOVEL:
                        sharedPreferencesEditor.putBoolean(Constant.COMPETENCE_DIALOG_NOVEL_SHOW, false);
                        sharedPreferencesEditor.commit();
                        break;
                    case Constant.OBJECT_TYPE_DRAW:
                        sharedPreferencesEditor.putBoolean(Constant.COMPETENCE_DIALOG_DRAW_SHOW, false);
                        sharedPreferencesEditor.commit();
                        break;
                    case Constant.OBJECT_TYPE_COS:
                        sharedPreferencesEditor.putBoolean(Constant.COMPETENCE_DIALOG_COS_SHOW, false);
                        sharedPreferencesEditor.commit();
                        break;
                }
            }
        }
    };

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        if (response == null) {
            return;
        }
        if (response.getStatus() == Constant.STATUS_SUCCESS) {
            switch (handleType) {
                case PersonalNoteService.HANDLE_TYPE_IS_NEED_COMPETENCE_WINDOW:
                    JSONObject data = (JSONObject) response.getData();
                    try {
                        // 请求服务器是否可以弹窗
                        String isNeed = data.getString("need");
                        if (isNeed.equals("1") && competencePupopWindowSharedPreferences.getBoolean(PUPOPWINDOW_SAVE_KEY, true)) {
                            TipInfo info = new TipInfo();
                            switch (Integer.valueOf(mObjectType)) {
                                case Constant.OBJECT_TYPE_NOVEL:
                                    // 是否已经弹过窗
                                    if (competencePupopWindowSharedPreferences.getBoolean(Constant.COMPETENCE_DIALOG_NOVEL_SHOW , true)){
                                        competenceDialogShow(info , "听说有作者权限，作品会被介绍，申请来一发？ و•̀_•́)و)" , tipListener);

                                    }
                                    break;
                                case Constant.OBJECT_TYPE_DRAW:
                                    if (competencePupopWindowSharedPreferences.getBoolean(Constant.COMPETENCE_DIALOG_DRAW_SHOW , true)){
                                        competenceDialogShow(info , "听说有画师权限，作品会被介绍，申请来一发？ و•̀_•́)و)" , tipListener);

                                    }
                                    break;
                                case Constant.OBJECT_TYPE_COS:
                                    if (competencePupopWindowSharedPreferences.getBoolean(Constant.COMPETENCE_DIALOG_COS_SHOW , true)){
                                        competenceDialogShow(info , "听说有Cos权限，作品会被介绍，申请来一发？ و•̀_•́)و)" , tipListener);

                                    }
                                    break;
                            }
                        } else {
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        } else {
            if (response.getUser_msg() != null) {
                UIUtil.toastMessage(mContext, response.getUser_msg());
            } else {
                UIUtil.toastMessage(mContext, Constant.loadingFail);
            }

        }
        handleComtenceRequest();
    }

    /**
     * 权限申请弹窗
     * @param tipInfo           弹窗基本信息，此处可以为null
     * @param text              弹窗提示文案
     * @param tipListener      弹窗回调监听
     */
    private void competenceDialogShow(TipInfo tipInfo, String text , ResultTipListener tipListener) {
        if (tipInfo == null) {
            tipInfo = new TipInfo();
        }
        tipInfo.setTipContent(text);
        tipInfo.setTipListener(tipListener);
        CompetenceDialog dialog = new CompetenceDialog(mContext, R.style.updateDialog, tipInfo);

        dialog.show();

    }
}
